
$(document).ready(function () {
    $(".more-user-info-box").fadeOut(0);
    $(".btn-box").click(function () {
        $(".more-user-info-box").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });
});

$(function () {
    blogData();
    $(".dp-article-box").fadeOut(0);
    $(".js-add-dp-box,.js-hf-article-pl").click(function () {
        $(".dp-article-box").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });
})

/**
 * 博客列表
 */
function blogData() {
    $("#listDataBody").html("");
    layui.use('flow', function(){

        var flow = layui.flow;
        flow.load({
            elem: '#listDataBody'
            ,done: function(page, next){
                var $ = layui.jquery;
                var lis = [];
                $.get('/u/blogs/'+$("#userId").val(),{"number":page}, function(res){
                    if(res.page){
                        layui.each(res.page.records, function(index, item){
                            var banner = item.banner,abstracts = item.abstracts;
                            if(!banner){
                                banner = item.headimg;
                            }

                            lis.push('<div class="mod-b mod-art" >'+
                                '<a class="transition" href="/blog/'+item.blogId+'" target="_blank">'+
                                '<div class="mod-thumb ">'+
                                '<img class="lazy" src="'+banner+'" style="display: inline;height: 100%">'+
                                '</div></a>'+
                                '<div class="mob-ctt">' +
                                '<h3><a style="cursor: pointer;" href="/blog/'+item.blogId+'" class="transition" target="_blank">'+item.title.substr(0, 20)+'</a></h3>'+
                                '<div class="mob-author">'+
                                '<span class="time">'+item.insertDate+'</span>'+
                                '<i class="icon icon-read-count" title="阅读"></i><em>'+item.readCount+'</em>' +
                                '<i class="icon icon-fvr" title="收藏"></i><em>'+item.collectCount+'</em>' +
                                '<i class="icon icon-zan" title="点赞"></i><em>'+item.praiseCount+'</em>' +
                                '</div>'+
                                '<div class="mob-sub">'+abstracts+'</div>'+
                                '</div>'+
                                '</div>'
                            );
                        });

                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }
        });
    });
    $("#tweetData").removeClass("active");
    $("#blogData").addClass("active");
}

/**
 * 动弹列表
 */
function tweetData() {
    $("#listDataBody").html("");
    layui.use('flow', function(){

        var flow = layui.flow;
        flow.load({
            elem: '#listDataBody'
            ,done: function(page, next){
                var $ = layui.jquery;
                var lis = [];
                $.get('/u/tweets/'+$("#userId").val(),{"number":page}, function(res){
                    if(res.page){
                        var userMap = res.usersMap;
                        lis.push('<div class="pl-box-wrap " data-pid="" id="">');
                        layui.each(res.page.records, function(index, item){
                            var comments_length = item.comments.length;
                            var active_html ="",active_color="";
                            if(item.currentUserId){
                                active_html = "active",active_color="background-color: #FFFFFF;";
                            }
                            var tweet_html= '<div class="pl-box-top">'+
                                '<div class="dropdown pull-right">'+
                                '<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'+
                                '<span class="caret"></span>'+
                                '</button>'+
                                '<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">'+
                                '<li class="pl-report js-report-pl">举报</li>'+
                                '</ul>'+
                                '</div>'+
                                '<div class="author-info">'+
                                '<div class="author-face">'+
                                '<img src="'+item.headimg+'">'+
                                '</div>'+
                                '<span class="author-name">'+
                                '<a href="/u/'+item.username+'">'+item.username+'</a>'+
                                '<a href="/u/'+item.username+'" target="_blank"></a>'+
                                '</span>'+
                                '<span class="time">'+item.insertDate+'</span>'+
                                '</div>'+
                                '<div class="pl-content">'+item.content+'</div>';

                            if(comments_length>0){
                                tweet_html +='<br>'+
                                    '<div class="dp-list-box">';
                                for(var i in item.comments){
                                    var comment = item.comments[i];
                                    var user = userMap[comment.userId];
                                    var reply = userMap[comment.replyId];
                                    var reply_html ='';
                                    if(comment.replyId){
                                        reply_html ='<a href="/u/'+reply.username+'" target="_blank">@'+reply.username+'</a> ';
                                    }

                                    tweet_html +=
                                        '<div class="dl-user " style="margin-left: 20px;">'+
                                        '<ul>'+
                                        '<li>'+
                                        '<a href="/u/'+user.username+'" target="_blank">'+
                                        '<img src="'+user.headimg+'">'+
                                        '</a>'+
                                        '</li>'+
                                        '</ul>'+
                                        '<div class="one-pl-content">'+
                                        '<div class="pull-right time">'+comment.insertDate+'</div>'+
                                        '<p class="content">'+
                                        '<a href="/u/'+user.username+'"><span class="name">'+user.username+'</span></a>'+
                                        '<a href="#" target="_blank"></a>&nbsp;&nbsp;'+
                                        '<span class="author-content">'+reply_html+comment.content+'</span>'+
                                        '</p>'+
                                        '<div class="js-hf-article-pl">'+
                                        '<a onclick="showTextArea(this)"><span>回复</span></a>'+
                                        '</div>'+
                                        '<div class="hu-pl-box">'+
                                        '<textarea class="form-control" placeholder="请文明回复~" maxlength="255"></textarea>'+
                                        '<button class="btn btn-article js-article-dp" data-type="hf" onclick="replyTweetComment(this,'+item.tweetId+',\''+comment.userId+'\')">回复</button>'+
                                        '</div>'+
                                        '</div>'+
                                        '</div>'+
                                        '</div>';
                                }
                                tweet_html+='</div>';
                            }

                            tweet_html += '<div class="pl-box-btm">'+
                                '<div class="article-type pull-right">'+
                                '<ul>'+
                                '<li class="js-icon-like" data-type="like">'+
                                '<i class="icon icon-read-count "></i><span class="like">'+item.readCount+'</span></li>'+

                                '<li class="js-icon-like" data-type="like">'+
                                '<a style="cursor: pointer;" onclick="tweet_praise('+item.id+')"><i class="icon icon-like '+active_html+'" style="'+active_color+'"></i>' +
                                '<span class="like" id="tweetPraiseCount_'+item.id+'">'+item.praiseCount+'</span></a></li>'+
                                '</ul>'+
                                '</div>'+
                                '<a onclick="showPTextArea(this)"><div class="btn-dp transition js-add-dp-box"><i class="icon icon-dp"></i>我要点评</div></a>'+
                                '<div class="pl-form-box dp-article-box">'+
                                '<textarea class="form-control" placeholder="请文明回复~" maxlength="255"></textarea>'+
                                '<button class="btn btn-article js-article-dp" onclick="addTweetComment(this,'+item.tweetId+')">发表</button>'+
                                '</div>'+
                                '</div>'+
                                '</div><br><br>';

                            lis.push(tweet_html);
                        });
                        lis.push('</div>');
                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }
        });
    });
    $("#blogData").removeClass("active");
    $("#tweetData").addClass("active");
}

function showTextArea(obj) {
    if($(obj).parent().next('div').css("display")=='none'){
        $(obj).parent().next().css({"display":"block"});
        $(obj).next().slideToggle(400);
    }else{
        $(obj).parent().next().css({"display":"none"});
    }

}

function showPTextArea(obj) {
    if($(obj).next('div').css("display")=='none'){
        $(obj).next().css({"display":"block"});

    }else{
        $(obj).next().css({"display":"none"});
    }

}

/**
 * 回复评论
 */
function replyTweetComment(obj,tweetId,replyUserId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;

            var content = $(obj).parent().children('textarea').val();
            if(!content){
                layer.msg("回复内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/u/add/comment/'+tweetId,
                data:{"content":content,"replyUserId":replyUserId},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code==0) {
                        layer.msg("回复成功~",{icon:6});
                        tweetData();
                    }else{
                        layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}


/**
 * 发表评论
 */
function addTweetComment(obj,tweetId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;
            var content = $(obj).parent().children('textarea').val();
            if (!content) {
                layer.msg("评论内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/u/add/comment/' + tweetId,
                data: {"content": content},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code == 0) {
                        layer.msg("评论成功~",{icon:6});
                        tweetData();
                    }else{
                        layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}


function tweet_praise(tweetId) {
    if(!logined()){
        showLogin();
        return;
    }
    if($("#tweetPraiseCount_"+tweetId).parent().prev().hasClass("active")){
        tweet_praise_del(tweetId)
    }else{
        tweet_praise_add(tweetId);
    }
}

/**
 * 给该动弹点赞
 */
function tweet_praise_add(tweetId) {
    $.ajax({
        url: '/tweet/add/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#tweetPraiseCount_"+tweetId).text();
                $("#tweetPraiseCount_"+tweetId).text(Number(value)+Number(1));
                $("#tweetPraiseCount_"+tweetId).parent().prev().addClass("active").css({"background-color":"#FFFFFF"});
            }
        },error:function (d) {

        }
    });
}

/**
 * 取消点赞
 * @param tweetId
 */
function tweet_praise_del(tweetId) {
    $.ajax({
        url: '/tweet/cut/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#tweetPraiseCount_"+tweetId).text();
                $("#tweetPraiseCount_"+tweetId).text(Number(value)-Number(1));
                $("#tweetPraiseCount_"+tweetId).parent().prev().removeClass("active");
            }
        },error:function (d) {
        }
    });
}
